using System;
using Globals.States;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace UI
{
    public class GameplayUi : MonoBehaviour
    {
        [SerializeField] private Animator pausePanel;
        [SerializeField] private Button resumeButton;
        [SerializeField] private Button quitButton;

        private readonly int _openAnimatorKey = Animator.StringToHash("IsOpen");


        private void Start()
        {
            resumeButton.onClick.AddListener(Resume);
            quitButton.onClick.AddListener(Quit);
        }

        public void TogglePauseUI()
        {
            pausePanel.SetBool(_openAnimatorKey, !pausePanel.GetBool(_openAnimatorKey));

            if (!EventSystem.current)
            {
                return;
            }
            
            if (pausePanel.GetBool(_openAnimatorKey))
            {
                EventSystem.current.SetSelectedGameObject(resumeButton.gameObject);
            }
            else
            {
                EventSystem.current.SetSelectedGameObject(null);
            }
        }

        private void Resume()
        {
            GameState.Instance.SwitchToPreviousState();
        }

        private void Quit()
        {
            SceneManager.LoadScene(0);
        }
    }
}
